package com.lh.project.interceptor;

import com.lh.project.common.ErrorCode;
import com.lh.project.common.JwtUtil;
import com.lh.project.common.UserHolder;
import com.lh.project.exception.JWTException;
import com.lh.project.model.vo.UserVO;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SetUserHandleInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler){
        //获取token
        String token = request.getHeader("token");
        if(token==null||token.length()==0){
            //说明没有token，不用设置UserVO
            return true;
        }
        //将用户封装到UserHolder中
        UserVO user = JwtUtil.getUser(token);
        UserHolder.saveUserVO(user);
        System.out.println("正在登录的用户是："+user);
        return true;
    }
}
